<?php
namespace app\index\controller;
use think\Db;
class Index extends Base
{
    public function index()
    {
        if(is_post()){
    		$cur_page     = input('page');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $status= input('status');
            $app_id= input('app_id');
            $user_id= input('user_id');
            $sql = "
                select
                    [1]
                from
                    task a
                left join
                    user b
                on
                    a.send_user_id=b.id
                left join
                    user c
                on
                    a.to_user_id=c.id
                left join
                    app d
                on
                    a.app_id=d.id
                left join
                    (select * from task_time where id in (select max(id) from task_time group by task_id)) e
                on
                    a.id=e.task_id
                ";
            $param = [];
            $w = ['where'];
            if(!empty($min_time)){
                $w[] = ' and a.create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and a.create_time<?';
                $param[] = strtotime($max_time);
            }
            if ( !empty($keyword) ) {
                $w[] .= " and a.title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( $status != '100all' ) {
                $w[] .= " and a.status = ?";
                $param[] = $status;
            }
            if ( $app_id != '100all' ) {
                $w[] .= " and a.app_id = ?";
                $param[] = $app_id;
            }
            if ( $user_id != '100all' ) {
                $w[] .= " and a.to_user_id = ?";
                $param[] = $user_id;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = 12;
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*,d.title as app_title,b.name as sendname,c.name as toname,e.content as fankui";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by a.sort desc,a.id desc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['status'] = c('type.task_status',$v['status']);
                $rslist[$k]['sort']   = com_sort('task',$v['id'],$v['sort']);
                $rslist[$k]['end_time']    = com_formate_time($v['end_time']);
                $rslist[$k]['update_time'] = com_formate_time($v['update_time']);
                $rslist[$k]['action'] = com_btn_edit(url('index/edittask','id='.$v['id']),'','','反馈');
                $rslist[$k]['action']  .= com_btn_del("/index/index/delete/id/{$v['id']}", "您确定要删除【{$v['title']}】？");
            }
            $body_html = com_list_body($rslist, ["app_title","title","sendname","toname","end_time", "sort", "update_time", "fankui", "status", "action"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $user = Db::name('user')->column('name','id');
        $app = Db::name('app')->column('title','id');
        $d =[
            'user'=>$user,
            'app'=>$app,
            'status'=>c('type.task_status'),
            'w'=>[
                'app_id'=>input('app_id')!=NULL?input('app_id'):'100all',
                'user_id'=>input('user_id')!=NULL?input('user_id'):'100all',
                'status'=>input('status')!=NULL?input('status'):'100all',
            ],
        ];
        return view('',$d);
    }

    public function addapp()
    {
        if(is_post()){
            $d = input();
            Db::name('app')->insert($d);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert','suc_url'=>url('index/index')]);
        }
        return view();
    }

    public function adduser()
    {
        if(is_post()){
            $d = input();
            $d['pwd'] = com_password();
            Db::name('user')->insert($d);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert','suc_url'=>url('index/index')]);
        }
        return view();
    }

    public function addtask(){
        if(is_post()){
            $d = input();
            $touser = Db::name('user')->find($d['to_user_id']);
            $data = [
                'send_user_id'=>$this->userinfo['id'],
                'app_id' => $d['app_id'],
                'to_user_id' => $d['to_user_id'],
                'status' => $d['status'],
                'start_time' => $d['start_time']?strtotime($d['start_time']):0,
                'end_time' => $d['end_time']?strtotime($d['end_time']):0,
                'title' => $d['title'],
                'content' => $d['content'],
                'create_time'=>time(),
                'update_time'=>time(),
            ];
            if(isset($d['tx_sms'])){
                $data['tx_sms'] = $touser['phone'];
                //发短信
            }
            if(isset($d['tx_email'])){
                $data['tx_email'] = $touser['email'];
                //发邮件
            }
            $id = Db::name('task')->insertGetId($data);

            //时间线
            $data['task_id'] = $id;
            Db::name('task_time')->insert($data);
            com_check(['suc_msg'=>'添加任务成功!','suc_js'=>'alert','suc_url'=>url('index/index')]);
        }
        $user = Db::name('user')->column('name','id');
        $app = Db::name('app')->column('title','id');
        $d =[
            'user'=>$user,
            'app'=>$app,
            'status'=>c('type.task_status'),
        ];
        return view('',$d);
    }


    public function edittask($id){
        $info = Db::name('task')->find($id);
        if(is_post()){
            $d = post();
            $touser = Db::name('user')->find($d['to_user_id']);
            $data = [
                'id'=>$d['id'],
                'send_user_id'=>$info['send_user_id'],
                'app_id' => $d['app_id'],
                'to_user_id' => $d['to_user_id'],
                'status' => $d['status'],
                'start_time' => $d['start_time']?strtotime($d['start_time']):0,
                'end_time' => $d['end_time']?strtotime($d['end_time']):0,
                'title' => $d['title'],
                'content' => $d['content'],
                'update_time'=>time(),
            ];
            if(isset($d['tx_sms'])){
                $data['tx_sms'] = $touser['phone'];
                //发短信
            }
            if(isset($d['tx_email'])){
                $data['tx_email'] = $touser['email'];
                //发邮件
            }
            Db::name('task')->update($data);

            //时间线
            $data['send_user_id'] = $this->userinfo['id'];
            $data['task_id'] = $data['id'];
            unset($data['id']);
            $data['create_time'] = time();
            Db::name('task_time')->insert($data);
            com_check(['suc_msg'=>'反馈任务成功!','suc_js'=>'alert','suc_url'=>url('index/index')]);
        }
        $list = Db::name('task_time')->where('task_id',$id)->order("id desc")->select();
        $user = Db::name('user')->column('name','id');
        $app = Db::name('app')->column('title','id');
        $d =[
            'user'=>$user,
            'app'=>$app,
            'status'=>c('type.task_status'),
            'info'=>$info,
            'list'=>$list,
        ];
        $this->view->engine->layout(false);
        return $this->fetch('',$d);
    }

    #删除
    function delete($id){
      Db::name('task')->delete($id);
      Db::name('task_time')->where('task_id',$id)->delete();
      com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }
}
